package cn.wolfcode.web.controller;


import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {

    @Autowired
    private IMessageBoardService messageBoardService;



    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "messageBoard/list";
    }


    @RequestMapping("/reply")
    public String reply(Model model,Long id){
        MessageBoard messageBoard = messageBoardService.get(id);
        model.addAttribute("messageBoard", messageBoard);
        //获取回复条数
        int count = messageBoard.getMessageReplys().size();
        model.addAttribute("count", count);
        return "messageBoard/reply";
    }


    @RequestMapping("/delete")
    @RequiredPermission(name="留言板删除",expression="messageBoard:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            messageBoardService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(MessageBoard messageBoard){
        messageBoardService.save(messageBoard);
        return new JsonResult();
    }
}
